<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2016-8-16
 * Func   代理入口
 */
session_start();
header('Content-Type:text/html; charset=UTF-8');
date_default_timezone_set('PRC');

define('IN_YuLin', true);
define('TIMESTAMP', time());
define('IS_POST',(strtoupper($_SERVER['REQUEST_METHOD']) == 'POST' ? true : false));
define('AGENT_PATH', str_replace('\\', '/', dirname(__FILE__) . '/')); //	管理员目录
define('ROOT_PATH', str_replace('\\', '/', dirname(dirname(__FILE__)) . '/')); //根目录
define('DATA_PATH', ROOT_PATH . '/data/'); //数据目录
define('LIB_PATH', ROOT_PATH . '/lib/');  //类库文件

include_once LIB_PATH . 'common.inc.php';
require_once LIB_PATH . 'protect.php';
include_once LIB_PATH . 'common.func.php';

//	数据库
include(LIB_PATH.'class/Db.class.php');
$db = new DB($_config['db']);

//	模版引擎
include(LIB_PATH.'class/Ytemplate.class.php');
$tpl = new Ytemplate();
$tpl->templateCompile = DATA_PATH.'compile';
$tpl->templateDir = AGENT_PATH.'template';
$tpl->templateExt = 'tpl.php';

//  系统数组
$_Y = [];
$_Y['user'] = [];
$_Y['ip'] = GetIp();

//  m、c、a、用来判断权限
$m = NoBadStr($_REQUEST['m']);
$c = NoBadStr($_REQUEST['c']);
$a = NoBadStr($_REQUEST['a']);
!$m && $m = 'index';
!$c && $c = 'index';
!$a && $a = 'index';

//	一个账号只能在一个地方登录
if($_SESSION['agent'])
{
	$tmp = $db->getrow('SELECT * FROM '.Table('user').' WHERE username = \''.$_SESSION['agent']['username'].'\' AND status = 1 ORDER BY id DESC LIMIT 1');
	if($tmp['last_ip'] != $_Y['ip'] || $tmp['last_time'] != $_SESSION['agent']['dateline'])
    {
        unset($_SESSION['agent']);
    }
}

//  登录判断
if(!$_SESSION['agent']['id'])
{
    $m = 'index';
    $c = 'login';
}
else
{
    //  记录操作日志：api模块不记录
    if($m != 'api')
    {
        $tmp = [];
        $tmp['aid']     =   $_SESSION['agent']['id'];
        $tmp['mca']     =   $m.'/'.$c.'/'.$a;
        $tmp['get']     =   json_encode($_GET);
        $tmp['post']    =   json_encode($_POST);
        $tmp['dateline']=   TIMESTAMP;
        $tmp['ip']      =   $_Y['ip'];
        $db->add('agent_log',$tmp);
    }
}

$_uid = intval($_SESSION['agent']['id']);
$_Y['user'] = $_SESSION['agent'];

if(file_exists(AGENT_PATH.'module/'.$m.'/'.$c.'.inc.php'))
{
    include_once(AGENT_PATH.'module/'.$m.'/'.$c.'.inc.php');
    exit();
}

exit('No input file...');

?>